package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.egg.ylt.R;
import com.egg.ylt.Utils.StringSpanUtils;
import com.egg.ylt.pojo.NatatoriumByidEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sichaoqi on 2018/9/4.
 */

public class ADA_ShopCardList extends RecyclerView.Adapter<ADA_ShopCardList.CardViewHolder> {


    private Context mContext;
    private LayoutInflater mInflater;
    private List<NatatoriumByidEntity.CardListBean> mList;
    private OnBtnClickListener mListener;
    private OnItemClickListener mItemListener;

    public ADA_ShopCardList(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
    }

    public void setCardList(List<NatatoriumByidEntity.CardListBean> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }


    @Override
    public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CardViewHolder(mInflater.inflate(R.layout.item_shop_recycler_card, parent, false));
    }

    @Override
    public void onBindViewHolder(CardViewHolder holder, int position) {
        final NatatoriumByidEntity.CardListBean bean = mList.get(position);

        holder.itemCardTvName.setText(bean.getName());
        holder.itemCardTvNext.setText(bean.getTimes() + "次");
        holder.itemCardTvTime.setText("有效期:" + bean.getEndDate());
        holder.itemCardTvType.setText(StringSpanUtils.init().addForegroundColorHighLight(mContext.getResources().getColor(R.color.ylt_orange),"￥"+bean.getPrice())
                .addForegroundColorHighLight(mContext.getResources().getColor(R.color.grgray),"/"+bean.getTotalPrice())
                .setDeleteLineWithKeyAndPosition(bean.getTotalPrice()+"",1)
                .setScaleWithKeyAndPosition(bean.getTotalPrice()+"",1,0.8f).build());

        //开通此卡的按钮
        holder.itemCardTvOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onBtnClick(bean.getId(), bean.getName(), bean.getPrice()+"", bean.getEndDate(),
                        bean.getShopName(), bean.getImageUrl());
            }
        });

        //条目点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemListener.onItemClick(bean.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class CardViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_card_tv_name)
        TextView itemCardTvName;
        @BindView(R.id.item_card_tv_time)
        TextView itemCardTvTime;
        @BindView(R.id.item_card_tv_next)
        TextView itemCardTvNext;
        @BindView(R.id.item_card_tv_open)
        TextView itemCardTvOpen;
        @BindView(R.id.item_card_tv_type)
        TextView itemCardTvType;

        public CardViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public interface OnBtnClickListener {
        void onBtnClick(String cardId, String cardName, String price, String endDate, String shopName,
                        String cardImageUrl);
    }

    public void setOnBtnClickListener(OnBtnClickListener listener) {
        this.mListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(String cardId);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemListener = listener;
    }
}
